package p2p;

import java.util.LinkedList;
import java.util.List;

/** Class that contains and manages the files being shared.
 * @author Bruno
 */
public class P2PFileList {

	/** Adds a file to the file list.
	 * @param file File to be added.
	 * @return <b>true</b> if the operation succeeds.
	 */
	static boolean addFile(P2PFile file) {
		if(list.contains(file))
			return false;
		else {
			list.add(file);
			file.startThread();
			return true;
		}

		
	}

	/** Returns the number of files in the list.
	 * @return Number of files in the list.
	 */
	static int getNumFiles() {
		return list.size();
	}

	/** Returns the number of files in the list that are not paused.
	 * @return Number of not paused files in the list.
	 */
	static int getNumActiveFiles() {
		int count = 0;
		for(P2PFile file: list)
			if(file.getState() != P2PFile.STATE_PAUSED)
				count++;
		return count;
	}

	/** Returns all files whose names contain all the keywords present in the filter.
	 * @param filter String containing the keywords to search in the files names.
	 * @return Files that matched the filter.
	 */
	static List<P2PFile> searchFiles(String filter) {
		List<P2PFile> files = new LinkedList<P2PFile>();

		for(P2PFile f: list) {
			if(f.matchName(filter))
				files.add(f);
		}

		return files;
	}

	/** Returns the file with the indicated <i>index</i> from the list.
	 * @param index Index of the file in the list.
	 * @return Selected file (or null if invalid index).
	 */
	static P2PFile getFile(int index) {
		if(index < 0 || index >= list.size())
			return null;
		else
			return list.get(index);
	}

	/** Removes the file with the indicated <i>index</i> from the list.
	 * @param index Index of the file in the list.
	 */
	static void removeFile(int index) {
		if(index < 0 || index >= list.size())
			return;

        list.get(index).deleteIfIncomplete();
		list.remove(index);
	}

	/** Refreshes the "Progress" values in the "Files" list of frmP2P.
	 */
	static void refreshFilesTable() {
		int r = 0;
		for(P2PFile file: list) {
			Global.frmP2P.FilesTable.setValueAt(file.getTransferProgress() + "%", r, 3);
			r++;
		}
	}

    /** Returns a file whose hash matches the hash given as an
     * argument to this method
     * @param hash The hash to search
     * @return P2PFile object that represents a file with the same
     * hash as the one on the argument
     */
    static P2PFile getFileByHash(String hash) {
        for (P2PFile file: list) {
            if (file.getHash().equals(hash)) {
                return file;
			}
        }

        return null;
    }

	/** Return the index of the <i>file</i> in the list.
     * @param file Target file that is contained int the list of files.
     * @return Index of the file in the list.
     */
	static int getFileIndex(P2PFile file) {
		int index = 0;
		for(P2PFile f: list) {
			if(file == f)
				return index;
			index++;
		}

		return -1;
	}

    /** Delete files from system (not from the list) that have not been completely downloaded.<br>
     * Should be called before ending the program.
     */
    static void deleteIncompleteFiles() {
        for (P2PFile file: list) {
            file.deleteIfIncomplete();
        }
    }

	static private List<P2PFile> list = new LinkedList<P2PFile>();
}
